package com.xinlong.controller;

import com.xinlong.entity.Guide;
import com.xinlong.entity.product;
import com.xinlong.service.IGuideService;
import com.xinlong.service.productservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/Guide")
public class GuideController {

    @Autowired
    productservice productservice;

    @Autowired
    IGuideService iGuideService;


    @RequestMapping("/openSaveGuide.do")
    public ModelAndView openSaveGuide(){
        ModelAndView mv=new ModelAndView();
        List<product> list=productservice.findByproductStatus(1);
        System.out.println(list);

        mv.addObject("product",list);

        mv.setViewName("Product-GuideImgADD");
        return mv;
    }

    @RequestMapping(path = "/fileUpload2.do",method = RequestMethod.POST)
    public String fileUpload2(HttpServletRequest request,@RequestParam(value="file") MultipartFile[] file) throws Exception {
        System.out.println("Springmvc文件上传");
//
//        String path=request.getSession().getServletContext().getRealPath("/uploads/");
        String path=request.getSession().getServletContext().getRealPath("/pages/imgg/");
//        request.getSession().getServletContext().getInitParameter("/pages/imgg/");
        System.out.println(path);
        String routeid=request.getParameter("routeid");
        //判断路径是否存在
        File file1=new File(path);
        if(!file1.exists()){
            file1.mkdirs();
        }
        for (MultipartFile upload1:file){
            //说明上传文件项
            //获得文件名
            String filename=upload1.getOriginalFilename();
            //生成名称唯一值
            String uuid= UUID.randomUUID().toString().replace("-","");
            filename=uuid+"_"+filename;
            //完成文件上传
            upload1.transferTo(new File(path,filename));

            Guide ge=new Guide();
            ge.setPicture(filename);
            ge.setRouteid(Integer.parseInt(routeid));
            iGuideService.saveGuide(ge);

        }


        return "redirect:openSaveGuide.do";
    }


    @RequestMapping("/selectGuide.do")
    @ResponseBody
    public List<Guide> selecGuide(Integer id){
        List<Guide> li=iGuideService.AllGuide(id);
        return li;
    }





}
